# -*- coding: utf-8 -*-
# @Time    : 2020/1/12 13:57
# @Author  : Weymouth
# @Email   : 261229090@qq.com
# @File    : copycopy.py
# @Software: PyCharm
# ---------------------------------------------------

print("浅拷贝")
import copy

lst = ['str1', 'str2', 'str3']
sourcelst = ['str1', 'str2', 'str3', lst]
copylst = copy.copy(sourcelst)
print("原本地址")
print([id(ele) for ele in sourcelst])
print([id(ele) for ele in copylst])
print("当sourceLst列表发生变化，copyLst中存储的lst内存地址没有改变")
sourcelst.append('source')
copylst.append('copy')
print("->sourcelst: ", sourcelst)
print("->copylst: ", copylst)
print(id(sourcelst))  # 4
print(sourcelst)  # 5
print([id(ele) for ele in sourcelst])
print(id(copylst))  # 4
print(copylst)  # 5
print([id(ele) for ele in copylst])
print("sourceLst的第一个元素发生了变化。而copyLst还是存储了str1的地址，所以copyLst不会发生改变。")
print([id(ele) for ele in sourcelst])
print([id(ele) for ele in copylst])
sourcelst[0] = 'change'
print("->sourcelst: ", sourcelst)
print("->copylst: ", copylst)
print(id(sourcelst))  # 4
print(sourcelst)  # 5
print([id(ele) for ele in sourcelst])
print(id(copylst))  # 4
print(copylst)  # 5
print([id(ele) for ele in copylst])

print("以当lst发生改变的时候，sourceLst和copyLst两个列表就都发生了改变。")
print([id(ele) for ele in sourcelst])
print([id(ele) for ele in copylst])
lst.append('Append')
print("->sourcelst: ", sourcelst)
print("->copylst: ", copylst)
print(id(sourcelst))  # 4
print(sourcelst)  # 5
print([id(ele) for ele in sourcelst])
print(id(copylst))  # 4
print(copylst)  # 5
print([id(ele) for ele in copylst])
#####################################################################################3
C:\Anaconda3\python.exe
H: / python / venv / pizza.py
浅拷贝
原本地址
[40600440, 42294440, 42294664, 42394568]
[40600440, 42294440, 42294664, 42394568]
当sourceLst列表发生变化，copyLst中存储的lst内存地址没有改变
->sourcelst: ['str1', 'str2', 'str3', ['str1', 'str2', 'str3'], 'source']
->copylst: ['str1', 'str2', 'str3', ['str1', 'str2', 'str3'], 'copy']
42394504
['str1', 'str2', 'str3', ['str1', 'str2', 'str3'], 'source']
[40600440, 42294440, 42294664, 42394568, 30438600]
42394376
['str1', 'str2', 'str3', ['str1', 'str2', 'str3'], 'copy']
[40600440, 42294440, 42294664, 42394568, 4282776]
sourceLst的第一个元素发生了变化。而copyLst还是存储了str1的地址，所以copyLst不会发生改变。
[40600440, 42294440, 42294664, 42394568, 30438600]
[40600440, 42294440, 42294664, 42394568, 4282776]
->sourcelst: ['change', 'str2', 'str3', ['str1', 'str2', 'str3'], 'source']
->copylst: ['str1', 'str2', 'str3', ['str1', 'str2', 'str3'], 'copy']
42394504
['change', 'str2', 'str3', ['str1', 'str2', 'str3'], 'source']
[42404472, 42294440, 42294664, 42394568, 30438600]
42394376
['str1', 'str2', 'str3', ['str1', 'str2', 'str3'], 'copy']
[40600440, 42294440, 42294664, 42394568, 4282776]
以当lst发生改变的时候，sourceLst和copyLst两个列表就都发生了改变。
[42404472, 42294440, 42294664, 42394568, 30438600]
[40600440, 42294440, 42294664, 42394568, 4282776]
->sourcelst: ['change', 'str2', 'str3', ['str1', 'str2', 'str3', 'Append'], 'source']
->copylst: ['str1', 'str2', 'str3', ['str1', 'str2', 'str3', 'Append'], 'copy']
42394504
['change', 'str2', 'str3', ['str1', 'str2', 'str3', 'Append'], 'source']
[42404472, 42294440, 42294664, 42394568, 30438600]
42394376
['str1', 'str2', 'str3', ['str1', 'str2', 'str3', 'Append'], 'copy']
[40600440, 42294440, 42294664, 42394568, 4282776]

Process
finished
with exit code 0
###################################################################################